package lab.chapter;

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class CharTransition extends Transition{

    // 状态迁移条件
    protected CharSet condition = new CharSet();

    public CharTransition(CharSet charSet) {
        condition = charSet;
    }

    public CharTransition(int maxTimes) {
        this.maxTimes = maxTimes;
    }

    /**
     * 是否满足迁移条件
     * 1. 如果没有任何 condition，那么就代表epsilon，可以迁移
     * 2. 否则检查字符是否落在 CharRange 中。
     * @param character 匹配对象
     * @return bool
     */
    public boolean math(Character character) {
        if (isEpsilon()) return false;
        return condition.match(character);
    }

    @Override
    public boolean match(Object object) {
        return math((Character) object);
    }

    @Override
    public boolean isEpsilon() {
        return condition.isEmpty();
    }

    @Override
    public String toString() {
        if (isEpsilon()) {
            return "ε";
        } else {
            CharSet charSet = condition;
            if (charSet.subSets != null && charSet.subSets.size() >= 10) {
                charSet = charSet.getShorterForm();
            }
            return charSet.toString();
        }

    }
}
